import { call, put, takeEvery } from 'redux-saga/effects';
import * as actionTypes from '@src/model/actionType';
import * as API from '@src/api/index';
import { get as _get } from 'lodash';

function* fetchTxDetail(action) {
  const params = _get(action, ['params'], {});
  try {
    const data = yield call(API.getTxDetail, params);
    yield put({
      type: actionTypes.txDetail,
      payload: {
        SalePrice: _get(data, ['Data', 'SalePrice'], ''), // 促销价格
        Business: _get(data, ['Data', 'Business'], ''), // 合作商家
        Producer: _get(data, ['Data', 'Producer'], ''), // 生产商家
        Storage: _get(data, ['Data', 'Storage'], ''), // 库存量
        Spec: _get(data, ['Data', 'Spec'], ''), // 规格参赛
        Warehouse: _get(data, ['Data', 'Warehouse'], ''), // 仓库
        Delivery: _get(data, ['Data', 'Delivery'], ''), // 交货周期
        IsNew: _get(data, ['Data', 'IsNew'], true), // 是否为新货
        TxId: _get(data, ['Data', 'TxId'], ''), // 交易id
        TxHash: _get(data, ['Data', 'TxHash'], ''), // 交易hash
        BlockHash: _get(data, ['Data', 'BlockHash'], ''), // 区块hash
        BlockHeight: _get(data, ['Data', 'BlockHeight'], 0), // 区块高度
        Sender: _get(data, ['Data', 'Sender'], ''), // 交易发送者
        ContractName: _get(data, ['Data', 'ContractName'], ''), // 合约名
        ContractVersion: _get(data, ['Data', 'ContractVersion'], ''), // 合约版本号
        TxStatusCode: _get(data, ['Data', 'TxStatusCode'], ''), // 交易状态
        ContractResultCode: _get(data, ['Data', 'ContractResultCode'], ''), // 合约执行结果码
        RwSetHash: _get(data, ['Data', 'RwSetHash'], ''), // 合约读写集哈希
        ContractMethod: _get(data, ['Data', 'ContractMethod'], ''), // 方法名
        ContractParameters: _get(data, ['Data', 'ContractParameters'], ''), // 参数
        Timestamp: _get(data, ['Data', 'Timestamp'], 0), // 时间戳
        ContractResult: _get(data, ['Data', 'ContractResult'], ''), // 合约执行结果
      },
    });
  } catch (err) {
    console.error(err);
  }
}

const sagas = [takeEvery(actionTypes.fetchTxDetail, fetchTxDetail)];

export default sagas;
